package com.kaihong.template.ble.operation.callback;

import java.util.HashSet;
import java.util.Optional;
import java.util.Set;

/**
 * Callback function generator
 */
public class CallbackProvider<T> {
    private final Set<T> callbackSet;

    private CallbackProvider(T callback) {
        callbackSet = new HashSet<>();
        Optional.ofNullable(callback).ifPresent(callbackSet::add);
    }

    /**
     * create CallbackProvider instance
     *
     * @param callback callback
     * @param <T> T
     * @return CallbackProvider instance
     */
    public static <T> CallbackProvider<T> of(T callback) {
        return new CallbackProvider<>(callback);
    }

    /**
     * find instance from callbackSet
     *
     * @return Optional instance
     */
    public Optional<T> get() {
        return callbackSet.stream().findFirst();
    }
}
